package com.example.games.mygame;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

import com.example.games.core.CSprite;
import com.example.games.core.ResourceManager;
import com.example.games.core.ScreenUtil;

public class Arrow implements CSprite {
	Bitmap arrowBmp = ResourceManager.getMapImg("jian_normal");

	private int x, y;

	int circleX, circleY;

	double circleR;

	private double a, b;

	private float degrees;

	public Arrow(double a, double b, float degrees) {
		this.a = a;
		this.b = b;
		this.degrees = degrees;
		x = ScreenUtil.getScreenX(40);
		y = ScreenUtil.getScreenY(258);
	}

	@Override
	public void drawSelf(Canvas canvas, Paint paint) {
		// TODO Auto-generated method stub
		canvas.save();
		canvas.rotate(degrees, x, y);
		canvas.drawBitmap(arrowBmp, ScreenUtil.getScreenX(x),
				ScreenUtil.getScreenY(y), paint);
		canvas.restore();
	}

	@Override
	public void logic() {
		// TODO Auto-generated method stub
		x += 50;
		y = (int) (a * x + b);
	}

	@Override
	public int getX() {
		// TODO Auto-generated method stub
		return x;
	}

	@Override
	public int getY() {
		// TODO Auto-generated method stub
		return y;
	}

	@Override
	public int getSpriteWidth() {
		// TODO Auto-generated method stub
		return arrowBmp.getWidth();
	}

	@Override
	public int getSpriteHeight() {
		// TODO Auto-generated method stub
		return arrowBmp.getHeight();
	}

	@Override
	public boolean isTouched(float x, float y) {
		return false;
	}

	/**
	 * 检测是否碰撞敌人
	 * 
	 * @param enemy
	 * @return
	 */
	public boolean isCollidedEnemy(CSprite enemy) {
		float x1, x2;
		float y1, y2;
		double r1;
		double r2;
		x1 = enemy.getCircleX();
		y1 = enemy.getCircleY();
		r1 = enemy.getCircleR();
		x2 = circleX;
		y2 = circleY;
		r2 = circleR;
		double tem = Math.sqrt(Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2));
		if (tem <= (r1 + r2) / 2) {
			return true;
		}
		return false;
	}

	@Override
	public int getCircleX() {
		// TODO Auto-generated method stub
		return arrowBmp.getWidth() / 2 + x;
	}

	@Override
	public int getCircleY() {
		// TODO Auto-generated method stub
		return arrowBmp.getHeight() / 2 + y;
	}

	@Override
	public double getCircleR() {
		// TODO Auto-generated method stub
		return Math.sqrt(Math.pow(arrowBmp.getWidth() / 2, 2)
				+ Math.pow(arrowBmp.getHeight() / 2, 2));
	}

	@Override
	public int getType() {
		// TODO Auto-generated method stub
		return 1;
	}

	@Override
	public void lostHp(int damage) {
		// TODO Auto-generated method stub
		hp -= damage;
	}

	private int hp = 1000;

	@Override
	public int getHp() {
		// TODO Auto-generated method stub
		return hp;
	}

	@Override
	public boolean isDead() {
		// TODO Auto-generated method stub
		return false;
	}

}
